package graphics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Point;

import controller.Constants;
/**
 * Triangle
 * @author Jie zhao(288654)
 *
 */
public class Triangle extends Polygon {
	public Triangle() {
		super();
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("triangle:");
		sb.append(getLineColor().getRGB()+":");
		sb.append((isFilled()?"1":"0") + ":");
		sb.append(getFilledColor().getRGB()+":");
		sb.append(getStroke().getLineWidth()+":");
		int[][] point = producePointsSet();
		for(int i=0;i<getNumofPoints();i++)
		{
			sb.append(point[0][i]+","+point[1][i]);
			if(i<getNumofPoints())sb.append(":");
		}
		return sb.toString();
	}

	@Override
	public void fromString(String str) {
		try{
		String[] data = str.split(":");
		if(!data[0].equals("triangle"))return;
		setLineColor(new Color(Integer.parseInt(data[1])));
		setFilled(data[2].equals("1"));
		setFilledColor(new Color(Integer.parseInt(data[3])));
		setStroke(new BasicStroke(Float.parseFloat(data[4]),BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND));
		String point[] = null;
		for(int i=5;i<data.length;i++)
		{
			point = data[i].split(",");
			addPoint(new Point(Integer.parseInt(point[0]),Integer.parseInt(point[1])));
		}
		}catch(Exception e)
		{
			if(Constants.DEBUG)e.printStackTrace();
		}
		finalize();
	}
	@Override
	public void addPoint(Point p) {
		if(isFinalized())return;
		if (getNumofPoints() < 3) {
			super.addPoint(p);
		}
		else if(getNumofPoints() >= 3)
		{
			finalize();
		}
		else
		{
			return;
		}
	}
	public static void main(String[] args) {
		Triangle c = new Triangle();
		c.addPoint(new Point(123,321));
		c.addPoint(new Point(12,31));
		c.addPoint(new Point(23,32));
		System.out.println(c);
		
		Triangle c2 = new Triangle();
		c2.fromString(c.toString());
		System.out.println(c2.getNumofPoints());
	}
}
